{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1*1=1\t\n",
      "1*2=2\t2*2=4\t\n",
      "1*3=3\t2*3=6\t3*3=9\t\n",
      "1*4=4\t2*4=8\t3*4=12\t4*4=16\t\n",
      "1*5=5\t2*5=10\t3*5=15\t4*5=20\t5*5=25\t\n",
      "1*6=6\t2*6=12\t3*6=18\t4*6=24\t5*6=30\t6*6=36\t\n",
      "1*7=7\t2*7=14\t3*7=21\t4*7=28\t5*7=35\t6*7=42\t7*7=49\t\n",
      "1*8=8\t2*8=16\t3*8=24\t4*8=32\t5*8=40\t6*8=48\t7*8=56\t8*8=64\t\n",
      "1*9=9\t2*9=18\t3*9=27\t4*9=36\t5*9=45\t6*9=54\t7*9=63\t8*9=72\t9*9=81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f\"{j}*{i}={i * j}\\t\", end=\"\")\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16 = 2*2*2*2"
     ]
    }
   ],
   "source": [
    "num = int(input(\"请输入一个正整数: \"))\n",
    "print(num, \"=\", end=\" \")\n",
    "if num < 2:\n",
    "    print(num)\n",
    "else:\n",
    "    factor = 2\n",
    "    while num > 1:\n",
    "        while num % factor == 0:\n",
    "            print(factor, end=\"\")\n",
    "            num //= factor\n",
    "            if num > 1:\n",
    "                print(\"*\", end=\"\")\n",
    "        factor += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "它是5位数\n",
      "逆序打印各位数字: 54321\n"
     ]
    }
   ],
   "source": [
    "num = input(\"请输入一个不多于6位的正整数:\")\n",
    "# 判断输入是否合法，是否为正整数且位数不超过6位\n",
    "if num.isdigit() and int(num) > 0 and len(num) <= 6:\n",
    "    print(f\"它是{len(num)}位数\")\n",
    "    print(\"逆序打印各位数字:\", num[::-1])\n",
    "else:\n",
    "    print(\"输入的内容不符合要求，请输入不多于6位的正整数\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "结果为: 98765424\n"
     ]
    }
   ],
   "source": [
    "a = input(\"请输入一个数字（0-9）:\")\n",
    "n = input(\"请输入相加的项数:\")\n",
    "# 判断输入是否合法，a是否是单个数字，n是否是正整数\n",
    "if a.isdigit() and len(a) == 1 and n.isdigit() and int(n) > 0:\n",
    "    a = int(a)\n",
    "    n = int(n)\n",
    "    sum_result = 0\n",
    "    for i in range(1, n + 1):\n",
    "        num = int(str(a) * i)\n",
    "        sum_result += num\n",
    "    print(f\"结果为: {sum_result}\")\n",
    "else:\n",
    "    print(\"输入的内容不符合要求，请重新输入\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该分数序列前8项之和为: 13.24374595992243\n"
     ]
    }
   ],
   "source": [
    "n = input(\"请输入项数N（N为正整数）:\")\n",
    "# 判断输入是否合法，是否为正整数\n",
    "if n.isdigit() and int(n) > 0:\n",
    "    n = int(n)\n",
    "    sum_result = 0\n",
    "    numerator = 2  # 分子\n",
    "    denominator = 1  # 分母\n",
    "    for _ in range(n):\n",
    "        sum_result += numerator / denominator\n",
    "        new_numerator = numerator + denominator\n",
    "        denominator = numerator\n",
    "        numerator = new_numerator\n",
    "    print(f\"该分数序列前{n}项之和为: {sum_result}\")\n",
    "else:\n",
    "    print(\"输入的内容不符合要求，请输入正整数作为项数\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4256\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "for i in range(1, 10000):\n",
    "    for j in range(1, 10000):\n",
    "        num = j ** 2 - 100 - 268\n",
    "        if i ** 2 == num + 100 and j ** 2 == num + 100 + 268:\n",
    "            print(num)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "第12个月兔子总数为144对\n"
     ]
    }
   ],
   "source": [
    "n = input(\"请输入要查看的月数（为正整数）:\")\n",
    "if n.isdigit() and int(n) > 0:\n",
    "    n = int(n)\n",
    "    if n == 1 or n == 2:\n",
    "        print(f\"第{n}个月兔子总数为1对\")\n",
    "    else:\n",
    "        month_1 = 1\n",
    "        month_2 = 1\n",
    "        for _ in range(3, n + 1):\n",
    "            current_count = month_1 + month_2\n",
    "            month_1 = month_2\n",
    "            month_2 = current_count\n",
    "        print(f\"第{n}个月兔子总数为{current_count}对\")\n",
    "else:\n",
    "    print(\"输入的内容不符合要求，请输入正整数作为月数\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "该矩阵对角线元素之和为: 30\n"
     ]
    }
   ],
   "source": [
    "# 初始化一个空的3x3矩阵\n",
    "matrix = []\n",
    "# 循环输入矩阵的每一行\n",
    "for i in range(3):\n",
    "    row = input(f\"请输入矩阵的第{i + 1}行（元素之间用空格隔开）:\").split()\n",
    "    # 将输入的字符串元素转换为整数元素\n",
    "    row = [int(x) for x in row]\n",
    "    matrix.append(row)\n",
    "\n",
    "# 计算主对角线元素之和\n",
    "sum_diagonal = 0\n",
    "for i in range(3):\n",
    "    sum_diagonal += matrix[i][i]\n",
    "\n",
    "# 计算副对角线元素之和\n",
    "sum_anti_diagonal = 0\n",
    "for i in range(3):\n",
    "    sum_anti_diagonal += matrix[i][2 - i]\n",
    "\n",
    "# 计算总的对角线元素之和\n",
    "total_sum = sum_diagonal + sum_anti_diagonal\n",
    "print(f\"该矩阵对角线元素之和为: {total_sum}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "移动后的整数序列为: [6, 6, 1, 2, 3, 8, 8]\n"
     ]
    }
   ],
   "source": [
    "n = input(\"请输入整数的个数n:\")\n",
    "m = input(\"请输入要移动的位置数m（m需小于等于n）:\")\n",
    "# 判断输入是否合法，n和m是否为正整数且m <= n\n",
    "if n.isdigit() and m.isdigit() and int(m) <= int(n) and int(n) > 0 and int(m) > 0:\n",
    "    n = int(n)\n",
    "    m = int(m)\n",
    "    num_list = []\n",
    "    # 输入n个整数\n",
    "    for _ in range(n):\n",
    "        num = input(\"请输入一个整数:\")\n",
    "        num_list.append(int(num))\n",
    "\n",
    "    # 进行位置移动\n",
    "    shifted_list = num_list[-m:] + num_list[:-m]\n",
    "\n",
    "    # 输出移动后的列表\n",
    "    print(\"移动后的整数序列为:\", shifted_list)\n",
    "else:\n",
    "    print(\"输入的内容不符合要求，请重新输入\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "根据输入计算的结果为: 0.9166666666666666\n"
     ]
    }
   ],
   "source": [
    "def sum_fractions(n):\n",
    "    result = 0\n",
    "    if n % 2 == 0:\n",
    "        for i in range(2, n + 1, 2):\n",
    "            result += 1 / i\n",
    "    else:\n",
    "        for i in range(1, n + 1, 2):\n",
    "            result += 1 / i\n",
    "    return result\n",
    "\n",
    "n = input(\"请输入一个整数:\")\n",
    "if n.isdigit() and int(n) > 0:\n",
    "    n = int(n)\n",
    "    sum_value = sum_fractions(n)\n",
    "    print(f\"根据输入计算的结果为: {sum_value}\")\n",
    "else:\n",
    "    print(\"输入的内容不符合要求，请重新输入\")\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
